import Vue from 'vue'
import Router from 'vue-router'
/* Layout */
import Layout from '@/layout'

/* Router Modules */

Vue.use(Router)

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    auth:['admin','device']      权限
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '仪表盘', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user role auth
 */
export const asyncRoutes = [

  {
    path: '/check_in',
    component: Layout,
    redirect: '/check_in/index',
    alwaysShow: true, // will always show the root menu
    name: 'CheckIn',
    meta: {
      title: '入住管理',
      icon: 'el-icon-office-building',
      auth: ['room_assign']
    },
    children: [
      {
        path: '/room_assign/index',
        component: () => import('@/views/room_assign/index'),
        name: 'AssignRoomIndex',
        meta: {
          title: '申请入住列表',
          auth: []
        }
      },
      {
        path: 'index',
        component: () => import('@/views/check_in/index'),
        name: 'CheckInRoomIndex',
        meta: {
          title: '入住列表',
          auth: ['room_type/index']
        }
      },
      {
        path: '/check-out/index',
        component: () => import('@/views/check-out/index'),
        name: 'CheckOutIndex',
        meta: {
          title: '退宿记录列表',
          auth: ['check-out/index']
        }
      }, {
        path: '/room_back_record/index',
        component: () => import('@/views/room_back_record/index'),
        name: 'RoomBackRecordIndex',
        meta: {
          title: '申请驳回列表',
          auth: ['roomLeaseRecord/index']
        }
      }
    ]
  },
  {
    path: '/room',
    component: Layout,
    redirect: '/room/index',
    alwaysShow: true, // will always show the root menu
    name: 'Room',
    meta: {
      title: '房间管理',
      icon: 'el-icon-s-home',
      auth: ['room']
    },
    children: [
      // {
      //   path: '/room_assign/index',
      //   component: () => import('@/views/room_assign/index'),
      //   name: 'RoomAssignIndex',
      //   meta: {
      //     title: '分配房间',
      //     auth: []
      //   }
      // },
      {
        path: '/housing/index',
        component: () => import('@/views/housing/index'),
        name: 'HousingIndex',
        meta: {
          title: '小区列表',
          auth: ['housing/index']
        }
      },
      {
        path: '/building/index',
        component: () => import('@/views/building/index'),
        name: ' BuildingIndex',
        meta: {
          title: '楼房列表',
          auth: ['building/index']
        }
      },
      {
        path: '/room/index',
        component: () => import('@/views/room/index'),
        name: 'RoomIndex',
        meta: {
          title: '房间列表',
          auth: ['room/index']
        }
      },
      // {
      //   path: '/housing/index',
      //   component: () => import('@/views/housing/index'),
      //   name: 'HousingIndex',
      //   meta: {
      //     title: '小区列表',
      //     auth: ['housing/index']
      //   }
      // },
      {
        path: '/roomType/index',
        component: () => import('@/views/roomType/index'),
        name: 'RoomTypeIndex',
        meta: {
          title: '房型设置',
          auth: ['roomType/index']
        }
      }
    ]
  },
  {
    path: '/charge',
    component: Layout,
    redirect: '/charge/index',
    alwaysShow: true, // will always show the root menu
    name: 'Charge',
    meta: {
      title: '收费管理',
      icon: 'el-icon-s-finance',
      auth: ['charge']
    },
    children: [
      {
        path: '/warning_record/index',
        component: () => import('@/views/warning_record/index'),
        name: 'WarningRecordIndex',
        meta: {
          title: '缴费预警',
          auth: []
        }
      },
      {
        path: 'index',
        component: () => import('@/views/charge/index'),
        name: 'ChargeIndex',
        meta: {
          title: '缴费单列表',
          auth: []
        }
      },
      {
        path: '/water_ele_price/index',
        component: () => import('@/views/water_ele_price/index'),
        name: 'WaterElePriceIndex',
        meta: {
          title: '设置水电费',
          auth: []
        }
      },
      {
        path: '/waterEleRecord/index',
        component: () => import('@/views/waterEleRecord/index'),
        name: 'WaterEleRecordIndex',
        meta: {
          title: '抄表记录',
          auth: []
        }
      },
      {
        path: '/cash_record/index',
        component: () => import('@/views/cash_record/index'),
        name: 'CashRecordIndex',
        meta: {
          title: '现金缴费记录',
          auth: []
        }
      }
      // {
      //   path: '/charge_room/index',
      //   component: () => import('@/views/charge_room/index'),
      //   name: 'ChargeRoom',
      //   meta: {
      //     title: '缴费情况',
      //     auth: ['room_type/index']
      //   }
      // }
    ]
  },
  {
    path: '/temperature',
    component: Layout,
    redirect: '/temperature/index',
    alwaysShow: true, // will always show the root menu
    name: 'Temperature',
    meta: {
      title: '测温管理',
      icon: 'el-icon-magic-stick',
      auth: ['charge']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/temperature/index'),
        name: 'TemperatureIndex',
        meta: {
          title: '测温列表',
          auth: []
        }
      },
      {
        path: '/measure_temperature/index',
        component: () => import('@/views/measure_temperature/index'),
        name: 'MeasureTemperature',
        meta: {
          title: '测温仪管理',
          auth: ['room/index']
        }
      }
    ]
  },
  {
    path: '/repair',
    component: Layout,
    redirect: '/repair/index',
    alwaysShow: true, // will always show the root menu
    name: 'Repair',
    meta: {
      title: '报修管理',
      icon: 'el-icon-warning-outline',
      auth: ['charge']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/repair/index'),
        name: 'RepairIndex',
        meta: {
          title: '报修列表',
          auth: []
        }
      }
    ]
  },
  {
    path: '/inspection',
    component: Layout,
    redirect: '/inspection/index',
    alwaysShow: true, // will always show the root menu
    name: 'Inspection',
    meta: {
      title: '巡检管理',
      icon: 'el-icon-zoom-in',
      auth: ['charge']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/inspection/index'),
        name: 'InspectionIndex',
        meta: {
          title: '巡检列表',
          auth: []
        }
      },
      {
        path: 'device',
        component: () => import('@/views/inspection_device/index'),
        name: 'InspectionDevice',
        meta: {
          title: '巡检设备',
          auth: []
        }
      },
      {
        path: 'type',
        component: () => import('@/views/pollingType/index'),
        name: 'PollingTypeIndex',
        meta: {
          title: '巡检类型',
          auth: []
        }
      }
    ]
  },

  {
    path: '/client',
    component: Layout,
    redirect: '/client/index',
    alwaysShow: true, // will always show the root menu
    name: 'Client',
    meta: {
      title: '租户端界面管理',
      icon: 'el-icon-s-home',
      auth: []
    },
    children: [
      {
        path: '/message/index',
        component: () => import('@/views/message/index'),
        name: 'MessageIndex',
        meta: {
          title: '通知列表',
          auth: []
        }
      },
      {
        path: '/news/index',
        component: () => import('@/views/news/index'),
        name: 'NewsIndex',
        meta: {
          title: '新闻列表',
          auth: []
        }
      },
      {
        path: '/adverts/index',
        component: () => import('@/views/adverts/index'),
        name: 'AdvertsIndex',
        meta: {
          title: '广告列表',
          auth: ['adverts/index']
        }
      },
      {
        path: '/surrounding/index',
        component: () => import('@/views/surrounding/index'),
        name: 'SurroundingIndex',
        meta: {
          title: '周边列表',
          auth: []
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    alwaysShow: true, // will always show the root menu
    name: 'User',
    meta: {
      title: '用户管理',
      icon: 'peoples',
      auth: ['user']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/user/index'),
        name: 'UserIndex',
        meta: {
          title: '租户列表',
          auth: ['user/index']
        }
      },
      {
        path: '/repair_user/index',
        component: () => import('@/views/repair_user/index'),
        name: 'RepairUserIndex',
        meta: {
          title: '维护员列表',
          auth: ['repair_user/index']
        }
      }
    ]
  },
  // {
  //   path: '/housing',
  //   component: Layout,
  //   redirect: '/housing/index',
  //   alwaysShow: true, // will always show the root menu
  //   name: 'Housing',
  //   meta: {
  //     title: '小区管理',
  //     icon: 'el-icon-office-building',
  //     auth: ['housing']
  //   },
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/housing/index'),
  //       name: 'HousingIndex',
  //       meta: {
  //         title: '小区列表',
  //         auth: ['housing/index']
  //       }
  //     },
  //     {
  //       path: '/building/index',
  //       component: () => import('@/views/building/index'),
  //       name: ' BuildingIndex',
  //       meta: {
  //         title: '楼房列表',
  //         auth: ['building/index']
  //       }
  //     }
  //   ]
  // },
  {
    path: '/system_role',
    component: Layout,
    redirect: '/system_role/index',
    alwaysShow: true, // will always show the root menu
    name: 'SystemRole',
    meta: {
      title: '角色管理',
      icon: 'el-icon-s-check',
      auth: ['system_role', 'system_auth']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/system_role/index'),
        name: 'SystemRoleIndex',
        meta: {
          title: '角色列表',
          auth: ['system_role/index']
        }
      },
      {
        path: '/system_auth/index',
        component: () => import('@/views/system_auth/index'),
        name: 'SystemAuthIndex',
        meta: {
          title: '权限列表',
          auth: ['system_auth/index']
        }
      }
    ]
  },
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/index',
    alwaysShow: true, // will always show the root menu
    name: 'Admin',
    meta: {
      title: '管理员',
      icon: 'el-icon-s-custom',
      auth: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/admin/index'),
        name: 'AdminIndex',
        meta: {
          title: '管理员列表',
          auth: ['admin/index']
        }
      }
    ]
  },
  {
    path: '/third_party',
    component: Layout,
    redirect: '/third_party/index',
    alwaysShow: true, // will always show the root menu
    name: 'ThirdParty',
    meta: {
      title: '第三方',
      icon: 'el-icon-s-custom',
      auth: ['ThirdParty']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/third_party/index'),
        name: 'ThirdPartyIndex',
        meta: {
          title: '快递柜列表',
          auth: ['third_party/index']
        }
      }
    ]
  },
  /*  {
      path: '/product',
      component: Layout,
      redirect: '/product/index',
      alwaysShow: true, // will always show the root menu
      name: 'Product',
      meta: {
        title: '套餐',
        icon: 'el-icon-shopping-cart-1',
        auth: ['product']
      },
      children: [
        {
          path: 'index',
          component: () => import('@/views/product/index'),
          name: 'ProductIndex',
          meta: {
            title: '套餐管理',
            auth: ['product/index']
          }
        }
      ]
    },*/
  {
    path: '/order',
    component: Layout,
    redirect: '/order/index',
    alwaysShow: true, // will always show the root menu
    hidden: true,
    name: 'Order',
    meta: {
      title: '订单',
      icon: 'el-icon-s-order',
      auth: ['order']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/order/index'),
        name: 'OrderIndex',
        meta: {
          title: '订单管理',
          auth: ['order/index']
        }
      },
      {
        path: 'index',
        query: {
          'filter[status]': '4,5',
          'op[status]': 'IN'
        },
        hidden: true,
        // redirect: '/order/index?filter[status]=4,5&op[status]=IN',
        component: () => import('@/views/order/refund'),
        name: 'OrderRefund',
        meta: {
          title: '退款管理',
          auth: ['order/index']
        }
      }
    ]
  },
  {
    path: '/attachment',
    component: Layout,
    redirect: '/attachment/index',
    alwaysShow: true, // will always show the root menu
    name: 'Attachment',
    meta: {
      title: '附件管理',
      icon: 'el-icon-paperclip',
      auth: ['attachment']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/attachment/index'),
        name: 'AttachmentIndex',
        meta: {
          title: '附件列表',
          auth: ['attachment/index']
        }
      }
    ]
  },
  {
    path: '/system_log',
    component: Layout,
    redirect: '/system_log/index',
    alwaysShow: true, // will always show the root menu
    name: 'SystemLog',
    meta: {
      title: '日志管理',
      icon: 'el-icon-tickets',
      auth: ['system_log']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/system_log/index'),
        name: 'SystemLogIndex',
        meta: {
          title: '日志列表',
          auth: ['system_log/index']
        }
      }
    ]
  },
  {
    path: '/system_config_tab',
    component: Layout,
    redirect: '/system_config_tab/index',
    alwaysShow: true, // will always show the root menu
    name: 'SystemConfigTab',
    meta: {
      title: '系统配置',
      icon: 'el-icon-s-tools',
      auth: ['system_config_tab', 'system_config', 'profile']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/system_config_tab/index'),
        name: 'SystemConfigTabIndex',
        meta: {
          title: '系统设置',
          auth: ['system_config_tab/index', 'system_config/index']
        }
      },
      {
        path: '/profile/index',
        component: () => import('@/views/profile/index'),
        name: 'ProfileIndex',
        meta: {
          title: '个人信息',
          auth: ['profile/update']
        }
      }
    ]
  },
  {
    path: '/about',
    component: Layout,
    redirect: '/about/index',
    alwaysShow: false, // will always show the root menu
    name: 'About',
    meta: {
      title: '关于',
      icon: 'el-icon-info',
      auth: []
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/about/index'),
        name: 'AboutIndex',
        meta: {
          title: '关于系统',
          auth: []
        }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
